package com.example.demo.controller.admin;

import com.example.demo.Util.Result;
import com.example.demo.Util.ResultUtil;
import com.example.demo.mapper.LabMap;
import com.example.demo.po.Lab;
import com.example.demo.po.Material;
import com.example.demo.service.LabService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.thymeleaf.util.StringUtils;

import java.util.List;

@RestController
@RequestMapping("/lab")
public class LabController {

    @Autowired
    LabMap labMap;

    @Autowired
    LabService labService;

    @PostMapping("/save")
    public Result SaveLab(@RequestBody Lab lab){

        if(lab==null){
            return ResultUtil.fail("Null Exception");
        }

        Result res =  labService.SaveLab(lab);
        return res;
    }

    @GetMapping("/get")
    public Result GetLabs(){

        return ResultUtil.success(labMap.GetLabs());
    }

    @GetMapping("/delete/{id}")
    public Result DeleteLabs(@RequestParam("id") int id){
        labMap.DeleteLabs(id);
        return ResultUtil.success("删除成功");
    }

    @PostMapping("/update")
    public Result UpdateLabs(@RequestBody  Lab lab){

        if(lab==null){
            return ResultUtil.fail("Null Exception");
        }

        labService.UpdateLabs(lab);
        return ResultUtil.success("更新成功");
    }

    @GetMapping("/search")
    public Result SearchLabsByName(@RequestParam("name") String name){
      if(StringUtils.isEmpty(name)){
          return ResultUtil.success(labMap.GetLabs());
      }

           return ResultUtil.success(labMap.SearchLabsByName("%"+name+"%"))  ;
    }

   @GetMapping("/getbycourse")
   public Result GetByCourse(){


       return ResultUtil.success(labMap.GetByCourse())  ;
   }


}
